package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.Issue;
import com.example.teesystem.entity.Library;
import com.example.teesystem.entity.vo.PageResponseVo;
import com.example.teesystem.entity.vo.library.*;
import com.example.teesystem.entityDto.IssueBriefMessage;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.mapper.IssueMapper;
import com.example.teesystem.service.LibraryService;
import com.example.teesystem.mapper.LibraryMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 12510
* @description 针对表【t_library】的数据库操作Service实现
* @createDate 2022-04-26 09:12:49
*/
@Service
@RequiredArgsConstructor
public class LibraryServiceImpl extends ServiceImpl<LibraryMapper, Library>
    implements LibraryService{

    private final LibraryMapper libraryMapper;
    private final IssueMapper issueMapper;

    @Override
    public Response uploadIssueLibrary(IssueLibraryInsertVo issueLibraryInsertVo) {
        Library library = new Library(issueLibraryInsertVo);
        int id = -1;
        libraryMapper.insert(library);
        id = library.getId();
        if(id != -1){
            return Response.ok();
        }else {
            return Response.error(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public Response uploadTipLibrary(TipLibraryInsertVo tipLibraryInsertVo) {
        Library library = new Library(tipLibraryInsertVo);
        int id = -1;
        libraryMapper.insert(library);
        id = library.getId();
        if(id != -1) {
            return Response.ok();
        } else {
            return Response.error(ResultCode.INSERT_ERROR);
        }
    }

    @Override
    public Response get(LibrarySelectVo librarySelectVo) {
        int issueType = 0, tipType = 1;
        Page<Library> libraryPage = new Page<>(librarySelectVo.getCur(), librarySelectVo.getSize());
        QueryWrapper<Library> wrapper = new QueryWrapper<>();
        wrapper.eq("student_id", MyHandlerInterceptor.getUid())
                .eq("course_id", librarySelectVo.getCourseId())
                 .eq("is_delete", 0);
        if(librarySelectVo.getLibraryType() == issueType){
            wrapper.eq("library_type", issueType)
                    .eq("is_delete", 0);
            Page<Library> myPage = this.page(libraryPage,wrapper);
            List<Library> libraryList =  myPage.getRecords();
            List<IssueLibraryResponse> libraryResponses = new ArrayList<>();
            for(Library library : libraryList){
                IssueLibraryResponse issueLibraryResponse = new IssueLibraryResponse(library);
                Issue issue = issueMapper.getIssueMessageById(library.getIssueId());
                issueLibraryResponse.setIssueBriefMessage(new IssueBriefMessage(issue));
                libraryResponses.add(issueLibraryResponse);
            }
            return Response.ok(new PageResponseVo<>(libraryResponses, myPage.getCurrent(), myPage.getSize(), myPage.getPages()));
        }else if(librarySelectVo.getLibraryType() == tipType){
            wrapper.eq("library_type", tipType);
            Page<Library> myPage = this.page(libraryPage,wrapper);
            List<Library> libraryList =  myPage.getRecords();
            List<TipLibraryResponse> libraryResponses = new ArrayList<>();
            for(Library library : libraryList){
                TipLibraryResponse tipLibraryResponse = new TipLibraryResponse(library);
                libraryResponses.add(tipLibraryResponse);
            }
            return Response.ok(new PageResponseVo<>(libraryResponses, myPage.getCurrent(), myPage.getSize(), myPage.getPages()));
        }else {
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
    }

    @Override
    public Response delete(int id) {
        Library library = libraryMapper.selectById(id);
        if(library == null || library.getStudentId() != MyHandlerInterceptor.getUid()){
            return Response.error(ResultCode.PARAMETER_ERROR);
        }
        try{
            libraryMapper.delete(id);
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.UPDATE_MESSAGE_ERROR);
        }

    }
}




